<?php
/**
 * Created by PhpStorm.
 * User: wangp
 * Date: 2016/7/7
 * Time: 20:28
 */

namespace Home\Controller;
include_once __DIR__ . '/vendor/autoload.php';
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use EasyWeChat\Message\Video;

class wecontroller {
    public function autopayoption(){
        $options = [
            'debug' => true,
            'app_id' => 'wx1cd5b8a5438c2130',
            'secret' => 'd65240fc907d1aad8d3386ea60d2726e ',

            'token' => '',  //token
            'log' => [
                'level' => 'debug',
                'file'  => 'abc.log',
            ],
            'guzzle' => [
                'timeout' => 3.0,// 超时
                'verify' => false,
            ],
            'oauth' => [
                'scopes' => ['snsapi_base'],
                'callback' => 'wx/oauth_callback.php',
            ],
            'payment' => [
                'merchant_id'        => '1353103702',   //商户ID
                'key'                => 'ksJJKzMCYXiNO69Ud4P086z0hobLMkyj', //商户key
                'cert_path'          => __DIR__ . '/../cert1/apiclient_cert.pem', // 证书
                'key_path'           => __DIR__ . '/../cert1/apiclient_key.pem',      // 证书
                'notify_url'         => 'http://'.$_SERVER['SERVER_NAME'].'/index.php?s=/Home/Share/paycallback.html',       // 回调地址
            ]
        ];
        return $options;
    }
    public function autoauthoption(){
        $options = [
            'debug' => true,
            'app_id' => 'wx1cd5b8a5438c2130',
            'secret' => 'd65240fc907d1aad8d3386ea60d2726e ',
            'token' => 'w0Q52Arls2S9T2qlsRA215q09q2L9R2z',
            'aes_key' => 'Veg9MEUnmztEYnsJgniStH3ifk8O0RmlBQKzvUuwHXE',

            'log' => [
                'level' => 'debug',
                'file' => __DIR__ . '/log/' . date('Y-m-d') . '.log',
            ],
            'guzzle' => [
                'timeout' => 3.0, // 超时时间（秒）
                'verify' => __DIR__ . '/cert/rootca.pem',
            ],

            'oauth' => [
                'scopes'   => ['snsapi_base'],  //snsapi_base | snsapi_userinfo
                'callback' => 'http://'.$_SERVER['SERVER_NAME'].'/index.php?s=/Home/Share/authcallback.html',    //回调地址
            ]
        ];
        return $options;
    }

    //认证登录
    public function authlogin(){
        $options = $this->autoauthoption();
        $app = new Application($options);
        $oauth = $app->oauth;

        if(empty($_SESSION['wechat_user'])){
            $response = $oauth->redirect();
            return $response->send();
        }
        $userService = $app->user;
        return  json_decode(json_encode($userService->get($_SESSION['wechat_user']['id'])),true);
    }

    //认证页面
    public function oauthcallback(){
        $options = $this->autoauthoption();
        $app = new Application($options);
        $oauth = $app->oauth;
        $user = $oauth->user();
        $_SESSION['wechat_user'] = $user->toArray();
    }
    //发送消息
    public function sendtask(){
        $news = new News([
            'title'       => "享问提问信息",
            'description' => '...',
            'url'         => "http://www.baidu.com",
            // ...
        ]);

    }
    //微信直接访问
    public function server(){
        $options = [
            'debug' => true,
            'app_id' => 'wxb0c815afa200ac50',
            'secret' => '723078792089da0955473e72ce0a65a3',
            'token' => 'w0Q52Arls2S9T2qlsRA215q09q2L9R2z',

            'guzzle' => [
                'timeout' => 3.0, // 超时时间（秒）
                'verify' => false, // 关掉 SSL 认证（强烈不建议！！！）
            ],
            'log' => [
                'level' => 'debug',
                'file' => __DIR__ . '/log/' . date('Y-m-d') . '.log',
            ],
        ];
        $app = new Application($options);
        $server = $app->server;
        $server->setMessageHandler(function ($message) {
            $openid = $message->FromUserName;
            switch ($message->MsgType) {
                case 'event':
                    switch ($message->Event) {
                        case 'subscribe':   //订阅
                            return '';
                            break;
                        case 'unsubscribe': //取消订阅
                            break;
                        default:
                            return '';
                            break;
                    }
                    break;
                case 'text':    //用户发送文本消息
                    //do something

                    break;
            }
        });
        $response = $server->serve();
        return $response->send();
    }
}
